using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hotelservices;

namespace hotelservices.app_code.ds
{
    public class ReservasDS
    {

        public reservas getReservaById(int id)
        {
            HotelEntities entities = new HotelEntities();
            IQueryable<reservas> res = (from s in entities.reservas where s.idreserva == id select s);
            if (res.Count() > 0)
                return res.First();
            else
                return null;
        }

        public List<reservas> getReservasByAgencia(int id)
        {
            HotelEntities entities = new HotelEntities();
            IQueryable<reservas> res = (from s in entities.reservas where s.fk_agencia == id select s);
            if (res.Count() > 0)
                return res.ToList();
            else
                return null;
        }

        public List<reservas> GetReservas(int startIndex, int maxRows)
        {
            using (HotelEntities entities = new HotelEntities())
            {
                return (from reservas in entities.reservas
                        select reservas)
                            .OrderBy(reserva => reserva.fecha_ingreso)
                            .Skip(startIndex)
                            .Take(maxRows).ToList();
            }
        }

        public int GetReservasCount()
        {
            using (HotelEntities entities = new HotelEntities())
            {
                return entities.reservas.Count();
            }
        }

        public bool save(ReservasDC reserva)
        {
            HotelEntities entities = new HotelEntities();
            reservas aux = new reservas();
            aux.idreserva = reserva.Idreserva;
            aux.fecha_ingreso = reserva.Fecha_ingreso;
            aux.fecha_salida = reserva.Fecha_salida;
            aux.total = reserva.Total;
            aux.abono = reserva.Abono;
            aux.fk_agencia = reserva.Fk_agencia;
            aux.fk_usuario = reserva.Fk_usuario;
            entities.reservas.Add(aux);
            int res = entities.SaveChanges();
            if (res > 0)
                return true;
            else
                return false;
        }

        public bool update(ReservasDC reserva)
        {
            HotelEntities entities = new HotelEntities();
            IQueryable<reservas> res = (from s in entities.reservas where s.idreserva == reserva.Idreserva select s);
            if (res.Count() > 0)
            {
                reservas aux = res.First();
                aux.idreserva = reserva.Idreserva;
                aux.fecha_ingreso = reserva.Fecha_ingreso;
                aux.fecha_salida = reserva.Fecha_salida;
                aux.total = reserva.Total;
                aux.abono = reserva.Abono;
                aux.fk_agencia = reserva.Fk_agencia;
                aux.fk_usuario = reserva.Fk_usuario;
                int result = entities.SaveChanges();
                if (result > 0)
                    return true;
                else
                    return false;
            }
            return false;
        }

    }
}
